<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="teacher/teacher-common::commonCss(~{::title},~{},~{::script},~{::style})">
    <title>JAVA在线考试-申请管理</title>

    <script th:src="@{/laydate/laydate.js}"></script>
    <style>
        .body{
            background-color: #f8f8f8;
        }
    </style>

    <script type="text/html" id="bar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">审批</a>
    </script>
    <script th:inline="javascript">
        layui.use(['table','element','laydate','form'], function(){
            var table = layui.table;
            var element = layui.element;
            var laydate = layui.laydate;
            var form = layui.form;

            laydate.render({
                elem: '#search-applyTime'
                ,range: true
            });

            $("#searchBtn").click(function () {
                reload();
            });
            $("#resetBtn").click(function () {
                $("#searchApplyInfoForm")[0].reset();
                tableIns.reload({
                    where: {
                        "msg": "",
                        "code": "",
                        "data": {
                            "studentUsername": '',
                            "clazzName": '',
                            "type": 0,
                            "applyStartTime": '',
                            "applyEndTime": ''
                        }
                    }
                    , page: {
                        curr: 1
                    }
                });
            });

            var tableIns = table.render({
                elem: '#applyTable'
                , url: '/apply/listForPage'
                , contentType: 'application/json'
                , method: 'post'
                , where: {
                    "msg": "",
                    "code": "",
                    "data": {
                        "studentUsername": $('#search-studentUsername').val(),
                        "clazzName": $('#search-clazzName').val(),
                        "type": $('#search-type').val(),
                        "applyStartTime": $("#search-applyTime").val().split(" - ")[0],
                        "applyEndTime": $("#search-applyTime").val().split(" - ")[1] != null ? $("#search-applyTime").val().split(" - ")[1] : ""
                    }
                }
                , defaultToolbar: []
                , page: true
                , limits: [5, 10, 20]
                , limit: 10
                , cols: [
                    [
                        {type: 'checkbox', fixed: 'left'}
                        , {field: 'id', title: 'ID', hide: true}
                        , {field: 'studentId', title: 'studentId', hide: true}
                        , {field: 'studentUsername', title: '学生用户名'}
                        , {field: 'clazzId', title: 'clazzId', hide: true}
                        , {field: 'clazzName', title: '班级名'}
                        , {field: 'type', title: '状态', templet: function (d) {
                            var type = d.type;
                            if(type===-1){
                                return '<span style="color:red;">已拒绝</span>';
                            }else if(type===1){
                                return '<span style="color:green;">已通过</span>';
                            }else{
                                return '<span>进行中</span>';
                            }
                        }}
                        , {field: 'applyTime', title: '申请时间'}
                        , {field: 'finishTime', title: '完成时间'}
                        , {title: '操作', fixed: 'right', width: 135, toolbar: '#bar'}
                    ]
                ]
                ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": res.result.code, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.records //解析数据列表
                    };
                }
            });

            //监听工具条
            table.on('tool(applyTable)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case 'edit':
                        layer.open({
                            type: 1,
                            area: ['550px', '250px'],
                            shadeClose: true, //点击遮罩关闭
                            title: '审批',
                            btn: ['确定', '取消'],
                            content: $('#applyInfoEditFrame'),
                            success: function (index, layero) {
                                $("#applyInfoEditForm")[0].reset();
                                layui.form.render();
                            },
                            yes: function (index, layero) {
                                var type = $("#edit-type").val();
                                $.ajax({
                                    url : '/apply/update/'+data.id,
                                    type : 'post',
                                    contentType : 'application/json',
                                    dataType: 'json',
                                    data: JSON.stringify({
                                        "msg":"",
                                        "code":"",
                                        "data": {
                                            "studentId": data.studentId,
                                            "teacherId": data.teacherId,
                                            "clazzId": data.clazzId,
                                            "type": type*1
                                        }
                                    }),
                                    success : function(n) {
                                        if (n.result.status) {
                                            layer.msg("修改申请信息成功");
                                            layer.close(index);
                                            reload();
                                        }else {
                                            layer.msg("修改申请信息失败,"+n.result.msg);
                                        }
                                    },
                                    error: function(n) {
                                        layer.msg("修改申请信息失败,"+n.responseJSON.result.msg);
                                    }
                                });
                            },
                            end: function (res) {
                                $("#applyInfoEditFrame").css("display", 'none');
                            }
                        });
                        break;
                }
                ;
            });

            function reload() {
                tableIns.reload({
                    where: {
                        "msg": "",
                        "code": "",
                        "data": {
                            "studentUsername": $('#search-studentUsername').val(),
                            "clazzName": $('#search-clazzName').val(),
                            "type": $('#search-type').val(),
                            "applyStartTime": $("#search-applyTime").val().split(" - ")[0],
                            "applyEndTime": $("#search-applyTime").val().split(" - ")[1] != null ? $("#search-applyTime").val().split(" - ")[1] : ""
                        }
                    }
                    , page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            }

        });

    </script>
</head>
<body>
<div class="layui-layout layui-layout-admin">

    <div th:replace="teacher/teacher-common::header"></div>
    <div th:replace="teacher/teacher-common::side('apply')"></div>

    <div class="layui-body body">
        <div class="layui-fluid" style="padding: 15px;">
            <div class="layui-card">
                <form class="layui-form layui-card-header" style="padding: 15px" lay-filter="searchApplyInfoForm" id="searchApplyInfoForm">
                    <div class="layui-form-item">
                        <div class="layui-inline" style="width:150px;">
                            <input id="search-studentUsername" type="text" placeholder="学生用户名" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline" style="width:150px;">
                            <input id="search-clazzName" type="text" placeholder="班级名" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline" style="margin-right: 0;">
                            <div class="layui-form-mid">状态</div>
                            <div class="layui-input-inline">
                                <select id="search-type">
                                    <option value="0">进行中</option>
                                    <option value="-1">已拒绝</option>
                                    <option value="1">已通过</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <input type="text" id="search-applyTime" placeholder="申请时间" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline" style="float: right;">
                            <button type="button" class="layui-btn" id="searchBtn">
                                <i class="layui-icon layui-icon-search"></i> 搜索
                            </button>
                            <button  type="button" class="layui-btn" id="resetBtn">重置</button>
                        </div>

                    </div>
                </form>

                <div class="layui-card-body">
                    <div style="padding-bottom:10px;">
                        <table class="layui-hide" id="applyTable" lay-skin="line" lay-filter="applyTable">
                        </table>
                    </div>
                </div>

            </div>
        </div>

    </div>

    <div th:replace="teacher/teacher-common::footer"></div>
</div>

</body>
<div id="applyInfoEditFrame" style="display:none;">
    <form class="layui-form" lay-filter="applyInfoEditForm" id="applyInfoEditForm" style="padding-right:30px">
        <div class="layui-form-item" style="margin-top: 20px;">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block" >
                <select id="edit-type">
                    <option value='1'>通过</option>
                    <option value='-1'>拒绝</option>
                    <option value='0'>重新审批</option>
                </select>
            </div>
        </div>
    </form>
</div>
</html>